<!--
 * @Descripttion: 编辑打印机
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2020-09-30 10:32:19
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @LastEditTime: 2023-08-16 15:25:49
-->
<template>
  <div class="lb-ordering-editPrint">
    <top-nav />
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="subForm"
        :rules="subFormRules"
        ref="subForm"
        label-width="120px"
        class="basic-form"
      >
        <el-form-item label="分账功能" prop="status">
          <el-radio-group v-model="subForm.status">
            <el-radio :label="1">{{ $t('action.ON') }}</el-radio>
            <el-radio :label="0">{{ $t('action.OFF') }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <block v-if="subForm.status">
          <el-form-item label="appid" prop="appid">
            <el-input
              v-model="subForm.appid"
              placeholder="请输入appid"
            ></el-input>
          </el-form-item>
          <el-form-item label="api_key_live" prop="api_key_live">
            <el-input
              v-model="subForm.api_key_live"
              placeholder="请输入api_key_live"
            ></el-input>
          </el-form-item>
          <el-form-item label="api_key_test" prop="api_key_test">
            <el-input
              v-model="subForm.api_key_test"
              placeholder="请输入api_key_test"
            ></el-input>
          </el-form-item>
          <el-form-item label="rsa_private_key" prop="rsa_private_key">
            <el-input
              type="textarea"
              resize="none"
              :rows="10"
              v-model="subForm.rsa_private_key"
              placeholder="请输入rsa_private_key"
            ></el-input>
          </el-form-item>
          <el-form-item label="手续费承担人" prop="commission">
            <el-radio-group v-model="subForm.commission">
              <el-radio :label="0">平台</el-radio>
              <el-radio :label="1">提现人</el-radio>
            </el-radio-group>
            <lb-tool-tips
              >对接第三方支付，在提现时会产生技术服务费，每提现一笔收取一元手续费</lb-tool-tips
            >
          </el-form-item>
        </block>
        <el-form-item>
          <lb-button type="primary" @click="submitFormInfo" v-preventReClick>{{
            $t('action.submit')
          }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      navTitle: '',
      subForm: {
        status: 1,
        appid: '',
        api_key_live: '',
        api_key_test: '',
        rsa_private_key: '',
        commission: 0
      },
      subFormRules: {
        status: { required: true, type: 'number', message: '请选择是否开启分账功能', trigger: 'blur' },
        appid: { required: true, validator: this.$reg.isNotNull, text: 'appid', reg_type: 2, trigger: 'blur' },
        api_key_live: { required: true, validator: this.$reg.isNotNull, text: 'api_key_live', reg_type: 2, trigger: 'blur' },
        api_key_test: { required: true, validator: this.$reg.isNotNull, text: 'api_key_test', reg_type: 2, trigger: 'blur' },
        rsa_private_key: { required: true, validator: this.$reg.isNotNull, text: 'rsa_private_key', reg_type: 2, trigger: 'blur' },
        commission: { required: true, type: 'number', message: '请选择手续费承担人', trigger: 'blur' }
      }
    }
  },
  created () {
    this.getDetail()
  },
  methods: {
    async getDetail () {
      let { code, data } = await this.$api.adapay.configInfo()
      if (code !== 200) return
      for (let i in this.subForm) {
        this.subForm[i] = data[i]
      }
    },
    async submitFormInfo () {
      let { status } = this.subForm
      let flag = status === 0
      if (status === 1) {
        this.$refs['subForm'].validate(valid => {
          if (valid) {
            flag = true
          }
        })
      }
      let subForm = JSON.parse(JSON.stringify(this.subForm))
      if (flag) {
        await this.$api.adapay.configUpdate(subForm)
        this.$message.success(this.$t('tips.successSub'))
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-ordering-editPrint {
  width: 100%;
  .el-input {
    width: 400px;
  }
  .el-textarea {
    width: 600px;
  }
}
</style>
